﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Core;

namespace Portal.AssetTn.Model
{
    public class AssetMaterialCls
    {
        public string AssetMaterialId = "";
        public string AssetMaterialName = "";
    }

    public class AssetMaterialParser
    {
        public static AssetMaterialCls CreateInstance()
        {
            //tao 1 class trang....
            AssetMaterialCls oAssetMaterial = new AssetMaterialCls();
            return oAssetMaterial;
        }

        public static AssetMaterialCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            AssetMaterialCls oAssetMaterial = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oAssetMaterial;
        }


        public static AssetMaterialCls ParseFromDataRow(DataRow dr)
        {
            AssetMaterialCls oAssetMaterial = new AssetMaterialCls();
            try
            {
                if (dr["AssetMaterialId"] != DBNull.Value)
                {
                    oAssetMaterial.AssetMaterialId = (string)dr["AssetMaterialId"];
                }
            }
            catch { }
            try
            {
                if (dr["AssetMaterialName"] != DBNull.Value)
                {
                    oAssetMaterial.AssetMaterialName = (string)dr["AssetMaterialName"];
                }
            }
            catch { }
            

            return oAssetMaterial;
        }

        public static AssetMaterialCls[] ParseFromDataTable(DataTable dtTable)
        {
            AssetMaterialCls[] AssetMaterials = new AssetMaterialCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                AssetMaterials[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return AssetMaterials;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet("Asset");
            ds.Tables.Add("Info");
            ds.Tables["Info"].Columns.Add("AssetMaterialId");
            ds.Tables["Info"].Columns.Add("AssetMaterialName");
            return ds;
        }

        public static string GetXml(AssetMaterialCls oAssetMaterial)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                        oAssetMaterial.AssetMaterialId,
                        oAssetMaterial.AssetMaterialName,
                    });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            AssetMaterialCls oAssetMaterial = ParseFromDataRow(dr);
            string Xml = GetXml(oAssetMaterial);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            AssetMaterialCls[] AssetMaterials = new AssetMaterialCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                AssetMaterials[iIndex] = ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = GetXml(AssetMaterials);
            return Xml;
        }


        public static string GetXml(AssetMaterialCls[] AssetMaterials)
        {
            if (AssetMaterials == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                AssetMaterials = new AssetMaterialCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < AssetMaterials.Length; iIndex++)
            {
                AssetMaterialCls oAssetMaterial = AssetMaterials[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                     {
                         oAssetMaterial.AssetMaterialId,
                         oAssetMaterial.AssetMaterialName,
                    });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static AssetMaterialCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new AssetMaterialCls[0];
            }
            AssetMaterialCls[] AssetMaterials = new AssetMaterialCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                AssetMaterials[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return AssetMaterials;
        }
    }
}
